﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using TfsBranchLib2.Model;
using TfsComLib.TfsBranchLib2;

namespace TfsBranchLib2.View
{
    /// <summary>
    ///     Контрол для редактирования фильтра в ветоке в TFS
    /// </summary>
    public partial class SourceControlFilterEditBox : UserControl
    {
        private SourceControlFilterMV modelView;

        public SourceControlFilterEditBox()
        {
            InitializeComponent();
        }

        public SourceControlFilterEditBox(ISourceControlFilter sourceControlFilter) : this()
        {
            SetBindings(sourceControlFilter);
        }

        public ISourceControlFilter SourceControlFilter { get; set; }

        public void UpdateSourceControlFilterFromTfsItems()
        {
            modelView.SaveTfsItemsToSourceControlFilter();
            SourceControlFilter.SubPathListChanged();
        }


        /// <summary>
        ///     Initialize view
        /// </summary>
        /// <param name="sourceControlFilter">Source control filter interface</param>
        public void SetBindings(ISourceControlFilter sourceControlFilter)
        {
            SourceControlFilter = sourceControlFilter;
            if (sourceControlFilter == null) return;
            basePath.DataContext = sourceControlFilter;
            basePath.SetBinding(TextBlock.TextProperty, "BasePath.BasePath");
            modelView = new SourceControlFilterMV(sourceControlFilter);
            modelView.UpdateTreeSource();
            treeView.DataContext = modelView;
            treeView.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("TfsItems") {Mode = BindingMode.TwoWay});
        }

        private void BrowseButton(object sender, RoutedEventArgs e)
        {
            var dialog = new SourceControlBasePathSettingsWindow(null, SourceControlFilter.BasePath,
                "Select base path for filter");
            dialog.ShowDialog();
        }
    }
}